package com.spring.demo.redis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


//封装redisTemplate的方法
@Component
public final class RedisUtil {
    @Autowired
    public RedisTemplate<String, Object> redisTemplate;

    public Object getValue(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);

    }

    public boolean setKey(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean setKey(String key, Object value, Long timeout, TimeUnit unit) {
        try {
            redisTemplate.opsForValue().set(key, value, timeout, unit);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public void incr(String in) {
        redisTemplate.opsForValue().increment(in);
    }


}
